﻿using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CozeDto.WithdrawalDtos;
using CozeDomain.IServices;
using D.UtilFreesqlOrm;

namespace CozeWebApi
{
    /// <summary>
    /// 用户提现模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class WithdrawalController : BaseApiController
    {
        private readonly IWithdrawalService _withdrawalService;
        public WithdrawalController(IWithdrawalService withdrawalService)
        {
            _withdrawalService = withdrawalService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddWithdrawal"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddWithdrawal reqAddWithdrawal)
        {
            reqAddWithdrawal.CreateUser = Account;
            return await _withdrawalService.AddWithdrawalAsync(reqAddWithdrawal);
        }

        /// <summary>
        /// 删除，多个逗号隔开
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _withdrawalService.DeleteWithdrawalAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateWithdrawal"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateWithdrawal reqUpdateWithdrawal)
        {
            reqUpdateWithdrawal.Id = id;
            reqUpdateWithdrawal.UpdateUser = Account;
            return await _withdrawalService.UpdateWithdrawalAsync(reqUpdateWithdrawal);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepWithdrawalInfo>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = LoginId;
            }
            return await _withdrawalService.GetWithdrawalAsync(id);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepWithdrawalInfo>>> Pages([FromBody] ReqQueryWithdrawalPage reqQueryWithdrawalPage)
        {
            return await _withdrawalService.QueryWithdrawalPagesAsync(reqQueryWithdrawalPage);
        }
    }
}
